package drt.toolbox.screen;

import java.awt.image.BufferedImage;
import java.util.Arrays;


public class Checksum {

	public int aX;
	public int aY;
	public int bX;
	public int bY;
	private int color;

	public Checksum() {
		aX = 0;
		aY = 0;
		bX = 0;
		bY = 0;
		color = 0;
	}
	
	public Checksum(int aX, int aY, int bX, int bY, String color) {
		this.aX = aX;
		this.aY = aY;
		this.bX = bX;
		this.bY = bY;
		this.color = 0;
	}

	public Checksum(int aX, int aY, int bX, int bY, BufferedImage image) {
		this.aX = aX;
		this.aY = aY;
		this.bX = bX;
		this.bY = bY;
		int prod = 0;
		for (int x = 0; x < (bX-aX); x++){
			int sum = 0;
			for(int y = 0; y < (bY-aY); y++){
				int color = image.getRGB(aX + x, aY + y);
				if (color == -16777216) {
					sum = sum + (int) Math.pow(2, y);
				}
			}
			prod = prod + sum * x;
		}
		this.color = prod;
	}
	
	public int getColor() {
		return color;
	}

	@Override
	public boolean equals(Object obj) {
		Checksum other = (Checksum) obj;
		if (getColor() == other.getColor()) {
			return true;
		}
		return false;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("C[" + aX + "," + aY + "," + bX + "," + bY + "," + color + "]");
		return sb.toString();
	}
}
